// 导入express
const express = require('express');
const path = require('path')
const fs = require('fs');


// 创建应用
const app = express();

function accessLogMiddleware(req, res, next) {

    const { url, ip } = req;
    fs.appendFileSync(path.resolve(__dirname, './access.log'), `${url} :${ip}\r\n`)
    next();
}

app.use(accessLogMiddleware)

// 创建路由
app.get('/home', (req, res) => {

    res.send('home')
})

// 创建路由
app.get('/admin', (req, res) => {

    res.send('admin')
})

app.all('*', (req, res) => {
    res.send('404')
})


// 监听端口，启用服务
app.listen(3000, () => {
    console.log('服务已启动，port 3000 正在监听...');
})

